<?php
   include 'cls_connection.php';
   class cls_user {
   var $connection;  
   
   function deleteFilesUsers($user_id,$image_id){
	$this->connection = new cls_connection ();
		   if ($this->connection->open_connection() == 1)
		   {
				$sql = "DELETE  FROM image_users  where image_id='$image_id' and user_id=$user_id";
				$result = mysql_query($sql) or die($sql); 
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
			} 
		  else
		  return -1;
	mysql_free_result($sql);
	}
   
   
   //falta verificar si en realidad crea la tabla y el performance que tendra la aplicacion
 
	function getUserDataByCriteria ($user_id){
	$this->connection = new cls_connection ();
		   if ($this->connection->open_connection() == 1)
		   {
				$sql = "select name,user_id from users where user_id <> $user_id and user_id not in 
				(select user_friend_id from user_friends where user_id=$user_id and user_friend_id=users.user_id) and 
				user_id not in (select user_id from user_friends WHERE user_friend_id=$user_id)";
				$result = mysql_query($sql) or die($sql); 
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
			} 
		  else
		  return -1;
	mysql_free_result($sql);
	}
	
	function delete_friend($my_id,$user_friend_id){
		$this->connection = new cls_connection ();
		   if ($this->connection->open_connection() == 1)
		   { 
			$sql = "DELETE  FROM user_friends  where user_id=$my_id or user_friend_id=$my_id and user_friend_id=$user_friend_id or user_id=$user_friend_id";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
			} 
		  else
		  return -1;
	mysql_free_result($sql);
	} 
	
	
	function blocked_friend($user_id,$user_friend_id,$value){
		$this->connection = new cls_connection ();
		   if ($this->connection->open_connection() == 1)
		   { 
			$sql = "UPDATE user_friends set blocked=$value where user_id=$user_id and user_friend_id=$user_friend_id and admitted=1";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
			} 
		  else
		  return -1;
	mysql_free_result($sql);
	}   
   
    function request_friendship($user_id,$user_friend_id){
		$this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "UPDATE user_friends set admitted=1 where user_id=$user_friend_id and user_friend_id=$user_id and admitted!=1";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);
   }
   
   //realiza una insercion cuando el usuario da clic sobre algun usuario, quedando pendiente la aceptacion del otro usuario
   function followFriend($user_id,$user_friend_id)
	{	
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO user_friends(user_id,user_friend_id) VALUES ('$user_id','$user_friend_id')";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);
}

function isFriendPetition($id){
   $this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "select * from user_friends where user_friend_id=$id and admitted!=1 ";
         $result = mysql_query($query) or die($query);
		$num_rows = mysql_num_rows($result);
		 if($num_rows==0){
		 return -1;
		 }else{
		 return $result;
		 }
		}
		 else{
		   return -1;
		   }
   }

  function isFriend($user_id){
   $this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "select users.user_id from users where users.user_id <> $user_id and users.user_id in 
(select user_friends.user_friend_id from user_friends where user_friends.user_id=$user_id  and user_friends.admitted=1)
or  users.user_id in (select user_friends.user_id from user_friends where user_friends.user_friend_id=$user_id and user_friends.admitted=1 ) ";
		//$query="select user_id from friends_user";
         $result = mysql_query($query) or die($query);
		 if(!$result){
		 die('Invalid query: ' . mysql_error());
		 }else{
		 return $result;
		 }
		}
		 else{
		   return -1;
		   }
   }
   
   
    //----------------------------------------------------------

		function identify ($id, $pas) {		   
		$this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "SELECT * FROM users where user_name = '" . $id . "' and password = '" . $pas . "';";
         $result = mysql_query($query) or die($query);
		 $num_rows = mysql_num_rows($result);
           return $result; 
        //return $query; 
       }
       else
            return -2;
             
   }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   //----------------------------------------------------------
 function change_estate ($id,$estado) {
		$this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "UPDATE users SET estado='$estado' where user_id= '" . $id . "';";
         $result = mysql_query($query) or die($query);
           return $result; 
        //return $query; 
       }
       else
            return -2;
             
   }
    function call($id,$parner) {
		$this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "UPDATE users SET estado='s', parner='$parner' where user_id= '" . $id . "';";
         $result = mysql_query($query) or die($query);
           return $result; 
        //return $query; 
       }
       else
            return -2;
             
   }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 function my_estate ($id) {
		$this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "select * from users where user_id= '" . $id . "';";
         $result = mysql_query($query) or die($query);
           return $result; 
        //return $query; 
       }
       else
            return -2;
             
   }
  function estate_parners ($id) {
		$this->connection = new cls_connection();
      if ($this->connection->open_connection() == 1) { 
         $query  = "select * from users where user_id!= '" . $id . "';";
         $result = mysql_query($query) or die($query);
		 $num_rows = mysql_num_rows($result);
           return $result; 
        //return $query; 
       }
       else
            return -2;
             
   }

function getCurrentTopic()
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
		   $sql="SELECT MAX(topic_id) as max FROM topics";
		   $res = mysql_query($sql) or die($sql); 
		   $reg = mysql_fetch_array($res);
		   $id=$reg['max'];
		   $sql = "SELECT topic_name,description FROM topics where topic_id='$id'";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
   
function getComments2($user_id,$inicio)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql=  "select distinct comments.user_id, comments.content,users.name from comments
					inner join users on comments.user_id=users.user_id
					 where  comments.user_id=users.user_id and comments.user_id in  
					(select users.user_id from users where users.user_id <> $user_id and users.user_id in  
					(select user_friends.user_friend_id from user_friends where user_friends.user_id=$user_id and  
					user_friends.admitted=1) or users.user_id in (select user_friends.user_id  
					from user_friends where user_friends.user_friend_id=$user_id and user_friends.admitted=1 )) 
					 or comments.user_id=$user_id  ORDER BY comments.id desc limit $inicio,3 ;";
$result = mysql_query($sql) or die($sql);
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}   
   
function getComments()
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "SELECT email,content FROM comments order by id desc";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}	

		function uploadComment($user_id,$comment)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO comments(user_id,email,content) VALUES ($user_id,(select email from users where user_id='$user_id'),'$comment')";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
	
	function userRegister($name,$lastName,$email,$password)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO users(name,last_name,email,password) VALUES ('$name,','$lastName','$email','$password')";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}


	function userUpdate($user_id,$name,$lastName,$email,$mobil)
{
		   $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "UPDATE users SET name='$name' ,last_name='$lastName' ,email='$email' ,mobil_num=$mobil where user_id=$user_id";
			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
			if(!$result){
			die('Invalid query: ' . mysql_error());
			}else{
				return $result;
			}    
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
	

function saveImage($user_id,$image_name,$jpg)
{
 $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
			$sql = "INSERT INTO image_users(user_id,image_name,image) VALUES('$user_id','$image_name','$jpg')"; 

			$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
		return $result;
	        } 
	      else
	      return -1;
mysql_free_result($sql);

}
	
function login ($admin_name,$password)
{
 $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
$sql = "SELECT * FROM administrator WHERE password = '$password' and admin_name='$admin_name'"; 

$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
 return $result;
	        } 
	      else
	       return -1;
mysql_free_result($sql);

}////////////////////////////////////////////

function loginUser ($email,$password)
{
 $this->connection = new cls_connection ();
           if ($this->connection->open_connection() == 1)
	       { 
$sql = "SELECT * FROM users WHERE email = '$email' and password='$password'"; 

$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
 return $result;
	        } 
	      else{
	       die('Invalid query: ' . mysql_error());
	      }
mysql_free_result($sql);

}


function getImageUser($user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM image_users WHERE user_id = '$user_id'"; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	 return $result;
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}

function getImageTumbs($image_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM image_users WHERE image_id = '$image_id' "; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	 return $result;
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}

function getUserData($user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "SELECT * FROM users WHERE user_id = '$user_id'"; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	 return $result;
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}


function setImageProfile($image_id,$user_id){
	$this->connection = new cls_connection ();
			   if ($this->connection->open_connection() == 1)
			   { 
	$sql = "update image_users  set image_profile='0' WHERE image_profile='1' and user_id=$user_id "; 
	
	$sql2 = "update image_users  set image_profile='1' WHERE image_id=$image_id  and user_id=$user_id"; 

	$result = mysql_query($sql) or die($sql);         //ejecuta la cansulta
	$result2 = mysql_query($sql2) or die($sql2);   
				} 
			  else
			   return -1;
	mysql_free_result($sql);
}

function getImageProfile($user_id){
	$this->connection = new cls_connection ();
	   if ($this->connection->open_connection() == 1)
	   { 
			$sql = "SELECT * FROM image_users WHERE user_id ='$user_id' and image_profile='1'"; 
			$result = mysql_query($sql) or die($sql); 
			$num_rows = mysql_num_rows($result);
				 if($num_rows==0){
				 return -1;
				 }else{
				 return $result;
				 }
		} 
	  else
	   return -1;
	mysql_free_result($sql);
}
			  	  	 
}
?>